package org.frog.sgs.util;

import java.util.Arrays;
import java.util.Scanner;

import org.apache.commons.lang3.ArrayUtils;

/**
 * @author duanxiangqi
 * @date 2012-10-27
 */
public enum SgsCmdUtil {
	instance;

	public int readInt() {
		while (true) {
			try {
				Scanner scanner = new Scanner(System.in);
				return scanner.nextInt();
			} catch (Exception e) {
				System.err.println("非法值，请重新输入。");
			}
		}
	}

	/**
	 * 从命令行读取一个指定取值内的int
	 * 
	 * @param values 指定取值
	 * @return
	 */
	public int readInt(int... values) {
		while (true) {
			try {
				Scanner scanner = new Scanner(System.in);
				int data = scanner.nextInt();
				if (ArrayUtils.contains(values, data)) {
					return data;
				} else {
					System.err.println("非法值，请重新输入。" + Arrays.toString(values));
				}
			} catch (Exception e) {
				System.err.println("非法值，请重新输入。" + Arrays.toString(values));
			}
		}
	}

	/**
	 * 从命令行读取一个String
	 * 
	 * @param values 指定取值
	 * @return
	 */
	public String readString() {
		while (true) {
			try {
				Scanner scanner = new Scanner(System.in);
				return scanner.next();
			} catch (Exception e) {
				System.err.println("非法值，请重新输入。");
			}
		}
	}

	/**
	 * 从命令行读取一个指定取值内的String
	 * 
	 * @param values 指定取值
	 * @return
	 */
	public String readString(String... values) {
		while (true) {
			try {
				Scanner scanner = new Scanner(System.in);
				String data = scanner.next();
				if (ArrayUtils.contains(values, data)) {
					return data;
				} else {
					System.err.println("非法值，请重新输入。" + Arrays.toString(values));
				}
			} catch (Exception e) {
				System.err.println("非法值，请重新输入。" + Arrays.toString(values));
			}
		}
	}
}
